################################################################################
### figures.R (21.11.2024)
### REPLICATION FILES OF TABLES AND FIGURES
### Brox, Enzo; Goller, Daniel
### Tournaments, Contestant Heterogeneity and Performance
################################################################################

#install and load packages
#install.packages("haven")
#install.packages("npcausal")
#install.packages("ggplot2")
#install.packages("np")
#install.packages("hrbrthemes")


library(haven)
library(ggplot2)
library(np)
library(npcausal)
library(hrbrthemes)


setwd("~/Replicationpackage")

################################################################################
# Load data

data_full<-as.data.frame(as.matrix(read_dta("DCHaP.dta")))  # Full data set
data_99 <- data_full[data_full$ratio_av_FU<1.22,]           # Restricting the sample to the 99% subsample without treatment outliers (N=4727)

################################################################################

                        ##                                             ##
                        #   Figure 1. Outcome and Treatment Variables   #
                        #   (a) Performance higher- and lower-ability   #
                        ##                                             ##


jpeg(file="figures/Figure1a.jpeg",width=1000,height=650,quality=100)
p <- ggplot(data=NULL) +
  geom_density(aes(x=data_full$Fav_av9),adjust=1.5, alpha=.2,fill="grey",linetype=1) +
  geom_density(aes(x=data_full$Und_av9),adjust=1.5, alpha=.4,fill="grey",linetype=2) +
  xlab("") +
  ylab("") +
  scale_x_continuous(
    name = "Performance"
  ) +
  theme_ipsum() +
  theme(
    axis.title.y = element_text(color = "black", size=44, family = "sans"),
    axis.title.x = element_text(color = "black", size=44, family = "sans")
  ) +
  theme(axis.text.x=element_text(angle=45, hjust=1,size=44, family = "sans"),
        axis.text.y=element_text(hjust=1,size=44, family = "sans"))
p
dev.off()
ggsave(file="figures/Figure1a.pdf", device = "pdf",width=13,height=10)

################################################################################

                          ##                                             ##
                          #   Figure 1. Outcome and Treatment Variables   #
                          #   (b) Treatment variable distribution         #
                          ##                                             ##


jpeg(file="figures/Figure1b.jpeg",width=1000,height=650,quality=100)
p <- ggplot(data=NULL) +
  geom_density(aes(x=data_full$ratio_av_FU),adjust=1.5, alpha=.2,fill="grey",linetype=1) +
  geom_vline(xintercept=1.222885, color="grey",linewidth=1.5,linetype=2) +
  xlab("") +
  ylab("") +
  scale_x_continuous(
    name = "Ability ratio"
  ) +
  theme_ipsum() +
  theme(
    axis.title.y = element_text(color = "black", size=44, family = "sans"),
    axis.title.x = element_text(color = "black", size=44, family = "sans")
  ) +
  theme(axis.text.x=element_text(angle=45, hjust=1,size=44, family = "sans"),
        axis.text.y=element_text(hjust=1,size=44, family = "sans"))
p
dev.off()
ggsave(file="figures/Figure1b.pdf", device = "pdf",width=13,height=10)

################################################################################
################################################################################
# Variable selection for NP Estimation
#Treatment Variable
a <- data_99$ratio_av_FU
#Covariates, separately for Underdog and Favorites
x_fav<-data.frame(data_99[,c(4,8,9,10,12,13,14,15,23,49,50,51,52,53,54,55)])
x_und<-data.frame(data_99[,c(4,8,9,11,12,13,14,15,23,49,50,51,52,53,54,55)])
#Outcomes
y_f9av<-data_99$Fav_av9
y_u9av<-data_99$Und_av9
y_t9av<-data_99$Total_av

### NP Estimation

cts.fav<-ctseff(y_f9av,a,x_fav,bw.seq=seq(0.02,0.12,length.out=12),sl.lib=c("SL.ranger"))
#load("figures/graphdata/cts.fav.Rdata") #Data can be loaded from the directory, in case of computational problems

cts.und<-ctseff(y_u9av,a,x_und,bw.seq=seq(0.02,0.12,length.out=12),sl.lib=c("SL.ranger"))
#load("figures/graphdata/cts.und.Rdata") #Data can be loaded from the directory, in case of computational problems

cts.tot<-ctseff(y_t9av,a,x_fav,bw.seq=seq(0.02,0.12,length.out=12),sl.lib=c("SL.ranger"))
#load("figures/graphdata/cts.tot.Rdata") #Data can be loaded from the directory, in case of computational problems

################################################################################

                    ##                                      ##
                    #               Figure 2 a)              #
                    # Lower-ability contestant performance   #
                    ##                                      ##


jpeg(file="figures/Figure2a.jpeg",width=1000,height=650,quality=100)
p <- ggplot(data=NULL) +
  geom_line(aes(y=cts.und$res$est,x=cts.und$res$a.vals),linetype=1,linewidth=1.5,color="darkblue") + 
  geom_line(aes(y=cts.und$res$ci.ll,x=cts.und$res$a.vals),linetype=2,linewidth=1.5,color="darkblue") + 
  geom_line(aes(y=cts.und$res$ci.ul,x=cts.und$res$a.vals),linetype=2,linewidth=1.5,color="darkblue") + 
  xlab("") +
  ylab("") +
  scale_y_continuous(
    limits = c(90, 110),
    name = "Underdog's performance"
  ) +
  scale_x_continuous(
    name = "Ability ratio"
  ) +
  theme_ipsum() +
  theme(
    axis.title.y = element_text(color = "black", size=44, family = "sans"),
    axis.title.x = element_text(color = "black", size=44, family = "sans")
  ) +
  theme(axis.text.x=element_text(angle=45, hjust=1,size=44, family = "sans"),
        axis.text.y=element_text(hjust=1,size=44, family = "sans"))
p
dev.off()
ggsave(file="figures/Figure2a.pdf", device = "pdf",width=13,height=10)
save(cts.fav, file=paste("figures/graphdata/cts.und.Rdata",sep=""))

################################################################################

                    ##                                      ##
                    #               Figure 2 b)              #
                    # Higher-ability contestant performance  #
                    ##                                      ##


jpeg(file="figures/Figure2b.jpeg",width=1000,height=650,quality=100)
p <- ggplot(data=NULL) +
  geom_line(aes(y=cts.fav$res$est,x=cts.fav$res$a.vals),linetype=1,linewidth=1.5,color="darkblue") + 
  geom_line(aes(y=cts.fav$res$ci.ll,x=cts.fav$res$a.vals),linetype=2,linewidth=1.5,color="darkblue") + 
  geom_line(aes(y=cts.fav$res$ci.ul,x=cts.fav$res$a.vals),linetype=2,linewidth=1.5,color="darkblue") + 
  xlab("") +
  ylab("") +
  scale_y_continuous(
    limits = c(90, 110),
    name = "Favorite's performance"
  ) +
  scale_x_continuous(
    name = "Ability ratio"
  ) +
  theme_ipsum() +
  theme(
    axis.title.y = element_text(color = "black", size=44, family = "sans"),
    axis.title.x = element_text(color = "black", size=44, family = "sans")
  ) +
  theme(axis.text.x=element_text(angle=45, hjust=1,size=44, family = "sans"),
        axis.text.y=element_text(hjust=1,size=44, family = "sans"))
p
dev.off()
ggsave(file="figures/Figure2b.pdf", device = "pdf",width=13,height=10)
save(cts.fav, file=paste("figures/graphdata/cts.fav.Rdata",sep=""))

################################################################################

                      ##                                      ##
                      #               Figure 2 c)              #
                      #           Mean performance             #
                      ##                                      ##


jpeg(file="figures/Figure2c.jpeg",width=1000,height=650,quality=100)
p <- ggplot(data=NULL) +
  geom_line(aes(y=cts.tot$res$est,x=cts.tot$res$a.vals),linetype=1,linewidth=1.5,color="darkblue") + 
  geom_line(aes(y=cts.tot$res$ci.ll,x=cts.tot$res$a.vals),linetype=2,linewidth=1.5,color="darkblue") + 
  geom_line(aes(y=cts.tot$res$ci.ul,x=cts.tot$res$a.vals),linetype=2,linewidth=1.5,color="darkblue") + 
  xlab("") +
  ylab("") +
  scale_y_continuous(
    limits = c(90, 110),
    name = "Mean performance"
  ) +
  scale_x_continuous(
    name = "Ability ratio"
  ) +
  theme_ipsum() +
  theme(
    axis.title.y = element_text(color = "black", size=44, family = "sans"),
    axis.title.x = element_text(color = "black", size=44, family = "sans")
  ) +
  theme(axis.text.x=element_text(angle=45, hjust=1,size=44, family = "sans"),
        axis.text.y=element_text(hjust=1,size=44, family = "sans"))
p
dev.off()
ggsave(file="figures/Figure2c.pdf", device = "pdf",width=13,height=10)
save(cts.fav, file=paste("figures/graphdata/cts.tot.Rdata",sep=""))

################################################################################
################################################################################

### Figure 3. Subsample effects
# The effects are estimated in STATA and can be replicated using the figures_estimates.do

## Data
data_het <- data.frame(Fav_av9 = c(5.591, 5.239, -0.051, 11.074, 5.377, 5.463),
                       Fav_av9se = c(4.582, 3.411, 4.571, 3.235, 4.198, 3.808),
                       Und_av9 = c(-12.414,-15.462, -8.850, -18.359, -16.840, -14.760),
                       Und_av9se = c(4.318, 4.290, 3.921, 5.298, 5.026, 4.393),
                       Fwin_av9 = c(1.337, 1.432, 1.093, 1.797, 1.462, 1.399),
                       Fwin_av9se = c(0.248, 0.218, 0.227, 0.194, 0.236, 0.222),
                       Sample = c(1, 0, 1, 0, 1, 0),
                       Variable=c(3.12,2.88,2.12,1.88,1.12,0.88))

data_het$Sample <- factor(data_het$Sample, levels = c(1, 0), labels = c("Low","High")) # Labelling values

data_het$Fav9_ci_up <-data_het$Fav_av9+1.64*data_het$Fav_av9se
data_het$Fav9_ci_ll <-data_het$Fav_av9-1.64*data_het$Fav_av9se

data_het$Und9_ci_up <-data_het$Und_av9+1.64*data_het$Und_av9se
data_het$Und9_ci_ll <-data_het$Und_av9-1.64*data_het$Und_av9se

data_het$Fwin9_ci_up <-data_het$Fwin_av9+1.64*data_het$Fwin_av9se
data_het$Fwin9_ci_ll <-data_het$Fwin_av9-1.64*data_het$Fwin_av9se

dw_append<-as.data.frame(data_het)

################################################################################

                      ##                                      ##
                      #               Figure 3 a)              #
                      # Lower-ability contestant performance   #
                      ##                                      ##


jpeg(file="figures/Figure3a.jpeg",width=1000,height=650,quality=100)
p<- dw_append %>%
  ggplot(aes(x=Variable, y=Und_av9, shape=Sample)) + 
  geom_hline(yintercept=-14.924, color="grey",linewidth=1.8) +
  geom_hline(yintercept=0, color="grey",linewidth=1.8,linetype = "dotted") +
  geom_point(size=6) +
  geom_errorbar(aes(ymin=Und9_ci_ll, ymax=Und9_ci_up),linewidth=1.5,width=0)+
  scale_x_discrete(name=NULL,limits=c("3","2","1"),
                   labels=c("Prize money", "Ability", "Experience"))+
  scale_y_continuous(name="Treatment Effect",limits = c(-30,10))+
  coord_flip()+
  theme_ipsum()+
  theme(axis.text.x=element_text(hjust=1,size=40, family = "sans"),
        axis.text.y=element_text(hjust=1,size=40, family = "sans")) + 
  theme(
    axis.title.y = element_text(color = "black", size=40, family = "sans"),
    axis.title.x = element_text(color = "black", size=40, family = "sans")) + 
  theme(
    legend.key.size = unit(1.5, 'cm'),
    legend.title = element_text(size=40, family = "sans"), 
    legend.text = element_text(size=40, family = "sans")) 
p
dev.off()
ggsave(file="figures/Figure3a.pdf", device = "pdf",width=13,height=10)

################################################################################

                    ##                                      ##
                    #               Figure 3 b)              #
                    # Higher-ability contestant performance  #
                    ##                                      ##


jpeg(file="figures/Figure3b.jpeg",width=1000,height=650,quality=100)
p<- dw_append %>%
  ggplot(aes(x=Variable, y=Fav_av9, shape=Sample)) + 
  geom_hline(yintercept=5.592, color="grey",linewidth=1.8) +
  geom_hline(yintercept=0, color="grey",linewidth=1.8,linetype = "dotted") +
  geom_point(size=6) +
  geom_errorbar(aes(ymin=Fav9_ci_ll, ymax=Fav9_ci_up),linewidth=1.5,width=0)+
  scale_x_discrete(name=NULL,limits=c("3","2","1"),
                   labels=c("Prize money", "Ability", "Experience"))+
  scale_y_continuous(name="Treatment Effect",limits = c(-10,20))+
  coord_flip()+
  theme_ipsum()+
  theme(axis.text.x=element_text(hjust=1,size=40, family = "sans"),
        axis.text.y=element_text(hjust=1,size=40, family = "sans")) + 
  theme(
    axis.title.y = element_text(color = "black", size=40, family = "sans"),
    axis.title.x = element_text(color = "black", size=40, family = "sans")) + 
  theme(
    legend.key.size = unit(1.5, 'cm'),
    legend.title = element_text(size=40, family = "sans"), 
    legend.text = element_text(size=40, family = "sans")) 
p
dev.off()
ggsave(file="figures/Figure3b.pdf", device = "pdf",width=13,height=10)

################################################################################

                ##                                      ##
                #               Figure A.1               #
                #     Higher-ability contestant win      #
                ##                                      ##


jpeg(file="figures/FigureA1.jpeg",width=1000,height=650,quality=100)
p<- dw_append %>%
  ggplot(aes(x=Variable, y=Fwin_av9, shape=Sample)) + 
  geom_hline(yintercept=1.424, color="grey",linewidth=1.8) +
  geom_hline(yintercept=0, color="grey",linewidth=1.8,linetype = "dotted") +
  geom_point(size=6) +
  geom_errorbar(aes(ymin=Fwin9_ci_ll, ymax=Fwin9_ci_up),linewidth=1.5,width=0)+
  scale_x_discrete(name=NULL,limits=c("3","2","1"),
                   labels=c("Prize money", "Ability", "Experience"))+
  scale_y_continuous(name="Treatment Effect",limits = c(-.1,2.2))+
  coord_flip()+
  theme_ipsum()+
  #ylim(-10,20) +
  theme(axis.text.x=element_text(hjust=1,size=40, family = "sans"),
        axis.text.y=element_text(hjust=1,size=40, family = "sans")) + 
  theme(
    axis.title.y = element_text(color = "black", size=40, family = "sans"),
    axis.title.x = element_text(color = "black", size=40, family = "sans")) + 
  theme(
    legend.key.size = unit(1.5, 'cm'),
    legend.title = element_text(size=40, family = "sans"), 
    legend.text = element_text(size=40, family = "sans")) 
p
dev.off()
ggsave(file="figures/FigureA1.pdf", device = "pdf",width=13,height=10)

################################################################################
################################################################################

            ##                                                 ##
            #                     Figure 4                      #
            #     Contest outcomes and betting market data      #
            ##                                                 ##

# Betting market data
smFwinprob   <- data_99$Fav_win_prob_odds

bw <- npregbw(smFwinprob~a,bws=c(0.05),
              bandwidth.compute=FALSE)
npout_sWP   <-npreg(bw, ckertype="gaussian")
sm_w_prob <- npout_sWP$mean
sm_w_prob_se <- npout_sWP$merr
x_fav<-data.frame(data_99[,c(10,49,50,51,52,53,54,55)])
sm_w_prob_ul <- sm_w_prob + 1.96*sm_w_prob_se
sm_w_prob_ll <- sm_w_prob - 1.96*sm_w_prob_se
df=data.frame(cbind(a,sm_w_prob,sm_w_prob_ul,sm_w_prob_ll))
df2=df[order(df$a), ]


# Outcome Variable
y_fwin<-data_99$Fav_win

# NP Estimation
cts.favw<-ctseff(y_fwin,a,x_fav,bw.seq=seq(0.075,0.12,length.out=12),sl.lib=c("SL.ranger"))
#load("figures/graphdata/cts.favw.Rdata") #Data can be loaded from the directory, in case of computational problems

# Graph

jpeg(file="figures/Figure4.jpeg",width=1000,height=650,quality=100)
p <- ggplot(data=NULL) +
  geom_line(aes(y=cts.favw$res$est,x=cts.favw$res$a.vals),linetype=1,linewidth=1.5,color="darkblue") + 
  geom_line(aes(y=cts.favw$res$ci.ll,x=cts.favw$res$a.vals),linetype=2,linewidth=1.5,color="darkblue") + 
  geom_line(aes(y=cts.favw$res$ci.ul,x=cts.favw$res$a.vals),linetype=2,linewidth=1.5,color="darkblue") + 
  geom_line(aes(y=df2$sm_w_prob, x=df2$a),linetype=1,linewidth=1.5,color="grey") +
  geom_ribbon(aes(ymin = df2$sm_w_prob_ll, ymax = df2$sm_w_prob_ul, x=df2$a), alpha = 0.2) +
  xlab("") +
  ylab("") +
  scale_y_continuous(
    name = "Favorite's winning probability"
  ) +
  scale_x_continuous(
    name = "Ability ratio"
  ) +
  theme_ipsum() +
  theme(
    axis.title.y = element_text(color = "black", size=44, family = "sans"),
    axis.title.x = element_text(color = "black", size=44, family = "sans")
  ) +
  theme(axis.text.x=element_text(angle=45, hjust=1,size=44, family = "sans"),
        axis.text.y=element_text(hjust=1,size=44, family = "sans"))
p
dev.off()
ggsave(file="figures/Figure4.pdf", device = "pdf",width=13,height=10)
save(cts.favw, file=paste("figures/graphdata/cts.favw.Rdata",sep=""))

################################################################################
################################################################################

                        ##                                                 ##
                        #                     Figure A.2                    #
                        #           Favorite win, shadow effect             #
                        ##                                                 ##

data_fullx<-data_full[!is.na(data_full$Upd_av_twin),]   #Sample Selection: Removing those matches without 'Future Opponent' in the competition
data_full_fut<-data_fullx[data_fullx$Upd_av_twin>70,]   #Removing 1 outlier

a_fut<-data_full_fut$Upd_av_twin
x_fut_fav<-data.frame(data_full_fut[,c(4,8,9,12,13,14,15)])
y_fut_fwin<-data_full_fut$Fav_win

cts.fut_w<-ctseff(y_fut_fwin,a_fut,x_fut_fav,bw.seq=seq(3,12,length.out=15),sl.lib=c("SL.ranger"))
#load("figures/graphdata/cts.fut_w.Rdata") #Data can be loaded from the directory, in case of computational problems


jpeg(file="figures/FigureA2.jpeg",width=1000,height=650,quality=100)
p <- ggplot(data=NULL) +
  geom_line(aes(y=cts.fut_w$res$est,x=cts.fut_w$res$a.vals),linetype=1,linewidth=1.5,color="darkblue") + 
  geom_line(aes(y=cts.fut_w$res$ci.ll,x=cts.fut_w$res$a.vals),linetype=2,linewidth=1.5,color="darkblue") + 
  geom_line(aes(y=cts.fut_w$res$ci.ul,x=cts.fut_w$res$a.vals),linetype=2,linewidth=1.5,color="darkblue") + 
  xlab("") +
  ylab("") +
  scale_y_continuous(
    name = "Favorite's winning probability"
  ) +
  scale_x_continuous(
    name = "Expected ability"
  ) +
  theme_ipsum() +
  theme(
    axis.title.y = element_text(color = "black", size=44, family = "sans"),
    axis.title.x = element_text(color = "black", size=44, family = "sans")
  ) +
  theme(axis.text.x=element_text(angle=45, hjust=1,size=44, family = "sans"),
        axis.text.y=element_text(hjust=1,size=44, family = "sans"))
p
dev.off()
ggsave(file="figures/FigureA2.pdf", device = "pdf",width=13,height=10)
save(cts.fut_w, file=paste("figures/graphdata/cts.fut_w.Rdata",sep=""))

################################################################################

#end